import {defineStore} from 'pinia'
import {useStateMixin} from 'st-store-pinia'
import {genGetDataByReqHandler} from 'st-common-req'
import {RoleType} from '../types/role-type.ts'
import {getRoles} from '../apis/role-api.ts'

/**
 * 系统角色管理相关信息状态管理仓库
 */
export const useSysRoleStore = defineStore('sysRoleStore', () => {
  // 表格是否为加载状态
  const {getData: getTableLoading, setData: setTableLoading} = useStateMixin<boolean>(false)

  // 表格数据
  const {getData: getTableData, setData: setTableData} = useStateMixin<RoleType[]>([])

  // 通过请求获取表格数据
  const getTableDataByReq = genGetDataByReqHandler<RoleType[]>({
    getDataApi: getRoles,
    setData: async (data) => setTableData(data),
    setLoading: setTableLoading,
  })

  return {
    getTableLoading,
    getTableData,
    getTableDataByReq,
  }
})
